package com.bosssoft.common.util;

import com.alibaba.fastjson.JSONObject;
import com.bosssoft.common.context.UserInfo;
import org.springframework.util.Assert;

import java.nio.charset.StandardCharsets;
import java.util.*;

import java.util.Base64;
import java.util.List;

public class TokenUtil {
    private static final Base64.Decoder DECODER = Base64.getDecoder();
    private static final Base64.Encoder ENCODER = Base64.getEncoder();

    public static List<String> parseToken(String token) {
        String[] jwtTokenItem = token.split("\\.");
        if (jwtTokenItem == null || jwtTokenItem.length < 2) {
            throw new RuntimeException("roleAuthorization 解析的token的第2个元素不存在");
        }
        String sign = token.substring(jwtTokenItem[0].length() + 1 + jwtTokenItem[1].length() + 1);
        return Arrays.asList(jwtTokenItem[0], jwtTokenItem[1], sign);
    }

    public static UserInfo getUserInfoFromToken(String token) {
        List<String> parts = parseToken(token);
        String payload = new String(DECODER.decode(parts.get(1)), StandardCharsets.UTF_8);
        JSONObject jsonObject = JSONObject.parseObject(payload);

        UserInfo userInfo = new UserInfo();
        userInfo.setUserId(jsonObject.getLong("userId"));
        userInfo.setUsername(jsonObject.getString("username"));
        userInfo.setRoles(jsonObject.getString("roles"));
        return userInfo;
    }

    public static String encoderToken(String jsonToken) {
        final String[] split = jsonToken.split("\\.");
        StringBuilder stringBuilder = new StringBuilder();
        for (String s : split) {
            final String tmp = encoder64(s);
            stringBuilder.append(tmp).append(".");
        }
        stringBuilder.deleteCharAt(stringBuilder.length() - 1);
        return stringBuilder.toString();
    }

    public static List<String> decryptToken(String token) {
        final String[] split = token.split("\\.");
        List<String> list = new ArrayList<>();
        for (String s : split) {
            final String s1 = decoder64(s);
            list.add(s1);
        }
        return list;
    }

    private static String decoder64(String baseString) {
        final byte[] textByte = baseString.getBytes(StandardCharsets.UTF_8);
        return new String(DECODER.decode(textByte), StandardCharsets.UTF_8);
    }

    private static String encoder64(String baseString) {
        return ENCODER.encodeToString(baseString.getBytes(StandardCharsets.UTF_8));
    }

    public static JSONObject parseTokenPayload(String token) {
        Assert.hasText(token, "token为空 parseTokenPayload 参数异常");
        String[] jwtTokenItem = token.split("\\.");
        if (jwtTokenItem == null || jwtTokenItem.length < 2) {
            throw new RuntimeException("roleAuthorization 解析的token的第2个元素不存在");
        }
        // 将playload转化为 JSONObject 对象返回
        String payload = new String(Base64.getDecoder().decode(jwtTokenItem[1]), StandardCharsets.UTF_8);
        return JSONObject.parseObject(payload);
    }

    private TokenUtil() {
    }

    public static List<String> parseToken2List(String token) {
        return parseToken(token);
    }
}
